package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.heima.apis.article.AuthorControllerApi;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/author")
public class AuthorController   implements AuthorControllerApi {

    @Autowired
    private AuthorService authorService;
    /**
     * 根据用户id查询作者信息
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/findByUserId/{id}")
    public ResponseResult findByUserId(@PathVariable("id") Integer id) {
        return authorService.findByUserId(id);
    }

    /**
     * 保存作者
     *
     * @param apAuthor
     * @return
     */
    @Override
    @PostMapping("/save")
    @Transactional
    @LcnTransaction
    public ResponseResult save(ApAuthor apAuthor) {
        authorService.save(apAuthor);
        //int a=1/0;
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据名称查询作者
     *
     * @param name
     * @return
     */
    @Override
    @GetMapping("/findByName/{name}")
    public ResponseResult findByName(@PathVariable  String name) {
        LambdaQueryWrapper<ApAuthor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApAuthor::getName,name);
        ApAuthor apAuthor = authorService.getOne(lambdaQueryWrapper);
        return ResponseResult.okResult(apAuthor);
    }


}
